package com.example.servlet;

import com.example.Service.NewsService;
import com.example.Service.UserService;
import com.example.pojo.News;
import com.google.gson.Gson;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "GetNewsInfoServlet", value = "/GetNewsInfoServlet")
public class GetNewsInfoServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*"); // 允许所有域名跨域，生产环境可指定具体域名
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
        String id =request.getParameter("id");
        System.out.println(id);
        NewsService ns= new NewsService();
        News news= ns.getNewsById(Integer.valueOf(id));
        Map<String,Object> m =new HashMap<>() ;
        m.put("code",200);
        m.put("msg","查询成功");
       m.put("data",news);
        Gson gson =new Gson();
        String json =gson.toJson(m);
        response.getWriter().append(json);
    }

    public void destroy() {
    }
}